//import FluentSQLite
import Vapor
import Foundation
import FluentMySQL

/// A single entry of a Todo list.
final class Todo: MySQLModel {
    typealias Database = MySQLDatabase
    /// See `Model.ID`
    typealias ID = Int
    static let idKey: IDKey = \.id
    /// The unique identifier for this `Todo`.
    var id: Int?
    
    var uid: String
    /// A title describing what this `Todo` entails.
    var title: String
    
    /// A content describing what this `Todo` entails.
    var content: String
    
    /// A title describing what this `Todo` entails.
    var updateTime: String

    /// Creates a new `Todo`.
    init(id: Int? = nil, title: String,content: String,uid: String) {
        self.id = id
        self.title = title
        self.uid = uid
        self.content = content
        self.updateTime = "\(Int(Date().timeIntervalSince1970))"
    }
    
    public static func todos(uid: String,req: Request) throws -> EventLoopFuture<[Todo]> {
        return try Todo.query(on: req).filter(\.uid == uid).all()
    }
    
    public static func getTodoByID(_ id: Int,req: Request) throws -> EventLoopFuture<Todo?> {
        return try Todo.query(on: req).filter(\.id == id).first()
    }
}

/// Allows `Todo` to be used as a dynamic migration.
extension Todo: Migration {

}

/// Allows `Todo` to be encoded to and decoded from HTTP messages.
extension Todo: Content { }

/// Allows `Todo` to be used as a dynamic parameter in route definitions.
extension Todo: Parameter { }
